﻿using System.Collections.Generic;
using System.Data;
using IDAL;
using Model;
using System;

namespace BLL
{
    public class Aging
    {
        private static readonly IAging dal = DALFactory.DataAccess.CreateAging();

        public bool AgingAndItem_Insert(AgingInfo ai, DataTable dt)
        {
            return dal.AgingAndItem_Insert(ai, dt);
        }

        public bool Aging_Insert(AgingInfo ai)
        {
            return dal.Aging_Insert(ai);
        }//已经被代替的函数

        public string Aging_InBoxManu(string agingID, string deviceName, string userID)
        {
            string str;
            //根据时效箱获取需要监控的批次
            AgingInfo ai = GetbyDeviceName(deviceName);
            if (!string.IsNullOrEmpty(ai.AgingID))
            {
                //if (ai.InBoxManuTime.AddMinutes(10) < DateTime.Now && DateTime.Now < ai.InBoxManuTime.AddMinutes(60 * (double)ai.TimeSpec + 30))
                if (ai.InBoxManuTime.AddMinutes(10) < DateTime.Now && string.IsNullOrEmpty(ai.TempEndManuUser))
                {
                    str = "上一批次进箱十分钟到出箱确认操作内不能再进此箱！";
                    return str;
                }
            }
            if (dal.Aging_InBoxManu(agingID, deviceName, userID))
            {
                str = "信息提交成功！";
                return str;
            }
            else
            {
                str = "信息提交失败，请联系管理员！";
                return str;
            }
        }

        public string Aging_InBoxQual(string agingID, string userID, int amount)
        {
            string str;
            if (string.IsNullOrEmpty(agingID))
            {
                str = "请先查询出相关批次信息，再确认！";
                return str;
            }
            else if (dal.Aging_InBoxQual(agingID, userID, amount))
            {
                str = "确认成功！";
                return str;
            }
            else
            {
                str = "数据写入失败，请联系管理员！";
                return str;
            }
        }

        public string Aging_TempStartManu(string agingID, string userID)
        {
            string str;
            if (string.IsNullOrEmpty(agingID))
            {
                str = "请先查询出相关批次信息，再确认！";
                return str;
            }
            else if (dal.Aging_TempStartManu(agingID, userID))
            {
                str = "确认成功！";
                return str;
            }
            else
            {
                str = "数据写入失败，请联系管理员！";
                return str;
            }
        }

        public string Aging_TempStartQual(string agingID, string userID)
        {
            string str;
            if (string.IsNullOrEmpty(agingID))
            {
                str = "请先查询出相关批次信息，再确认！";
                return str;
            }
            else if (dal.Aging_TempStartQual(agingID, userID))
            {
                str = "确认成功！";
                return str;
            }
            else
            {
                str = "数据写入失败，请联系管理员！";
                return str;
            }
        }

        public string Aging_PatrolManu(string agingID, string userID)
        {
            string str;
            if (string.IsNullOrEmpty(agingID))
            {
                str = "请先查询出相关批次信息，再确认！";
                return str;
            }
            else if (dal.Aging_PatrolManu(agingID, userID))
            {
                str = "确认成功！";
                return str;
            }
            else
            {
                str = "数据写入失败，请联系管理员！";
                return str;
            }
        }

        public string Aging_PatrolQual(string agingID, string userID)
        {
            string str;
            if (string.IsNullOrEmpty(agingID))
            {
                str = "请先查询出相关批次信息，再确认！";
                return str;
            }
            else if (dal.Aging_PatrolQual(agingID, userID))
            {
                str = "确认成功！";
                return str;
            }
            else
            {
                str = "数据写入失败，请联系管理员！";
                return str;
            }
        }

        public string Aging_TempEndManu(string agingID, string userID)
        {
            string str;
            if (string.IsNullOrEmpty(agingID))
            {
                str = "请先查询出相关批次信息，再确认！";
                return str;
            }
            else if (dal.Aging_TempEndManu(agingID, userID))
            {
                str = "确认成功！";
                return str;
            }
            else
            {
                str = "数据写入失败，请联系管理员！";
                return str;
            }
        }

        public bool Isvalid_TempEnd(string id, out string message)
        {
            AgingInfo agi = dal.GetbyAgingID(id);
            TimeSpan datediff = DateTime.Now - agi.TempStartManuTime;
            if ((decimal)datediff.TotalSeconds < agi.TimeSpec * 3600)
            {
                message = "恒温结束时间未到，客户端倒计时错误！请联系相关人员。";
                return false;
            }
            message = "";
            return true;
        }

        public string Aging_TempEndQual(string agingID, string userID, int amount)
        {
            string str;
            if (string.IsNullOrEmpty(agingID))
            {
                str = "请先查询出相关批次信息，再确认！";
                return str;
            }
            else if (dal.Aging_TempEndQual(agingID, userID, amount))
            {
                str = "确认成功！";
                return str;
            }
            else
            {
                str = "数据写入失败，请联系管理员！";
                return str;
            }
        }

        //以上为流程操作，以下为数据检索
        public bool Aging_ExistsID(string agingID)
        {
            return dal.Aging_ExistsID(agingID);
        }

        public bool Aging_ExistsValidID(string agingID)
        {
            return dal.Aging_ExistsValidID(agingID);
        }

        public string Aging_IsSecondAgingOK(DataTable dt)
        {
            return dal.Aging_IsSecondAgingOK(dt);
        }

        public string Aging_HasValidPostStep(string productTypeID, DataTable dt)
        {
            return dal.Aging_HasValidPostStep(productTypeID, dt);
        }

        public AgingInfo GetbyAgingID(string agingID)
        {
            return dal.GetbyAgingID(agingID);
        }

        public AgingInfo GetbyDeviceName(string deviceName)
        {
            return dal.GetbyDeviceName(deviceName);
        }

        public IList<string> GetIDsbyDeviceName(string deviceName, DateTime dt)
        {
            return dal.GetIDsbyDeviceName(deviceName, dt);
        }

        public AgingInfo GetAging_InBox(string agingID)
        {
            return dal.GetAging_InBox(agingID);
        }

        public IList<AgingInfo> GetLatestAgings()
        {
            return dal.GetLatestAgings();
        }

        public IList<AgingInfo> GetAgingsbyDT(DateTime starttime, DateTime stoptime)
        {
            return dal.GetAgingsbyDT(starttime, stoptime);
        }

        public IList<AgingInfo> GetAgingsbySN(string productSN)
        {
            return dal.GetAgingsbySN(productSN);
        }

        public IList<AgingInfo> GetPassAgingsbySN(string productSN)
        {
            return dal.GetPassAgingsbySN(productSN);
        }

        public string GetIDbySN(string productSN, string agingStep)
        {
            return dal.GetIDbySN(productSN, agingStep);
        }
    }
}
